﻿using System;
using EmbeddedSharp.Core;
using Zove.Runtime.Core;

namespace Zove.Scripting.Core.Functions {

    /// <summary>
    /// Returns the class name for an instance.
    /// </summary>
    [Function("type", "Returns the class name for an instance.",
        "circle c;\n\rtype=type(c);\n\recho(type);", "type==\"circle\"")]
    public class TypeNode : ZoveScriptingContextFuncNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public TypeNode()
            : base("type") {
        }

        /// <summary>
        /// Evaluate the expression
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            int count = Parameters.Count;

            if (count < 1) {
                throw new ArgumentException(
                    "usage: type(instance)");
            }

            string instanceName = Convert.ToString(Parameters[count - 1].Evaluate());
            ZoveObject instance = ZoveShellContext.GetInstance(instanceName);

            if (instance == null) {
                return false;
            }

            return instance.Model.Name;
        }
    }
}
